/** 
 * FileName:     Me.java 
 * @Description: TODO(用一句话描述该文件做什么) 
* All rights Reserved, Designed By hnkeystone 
 * Copyright:    Copyright(C) 2010-2011 
 * Company       坚磐科技 
 * @author:    冯辉 
 * @version    V1.0  
 * Createdate:         2017-3-29 下午6:18:11 
 * 
 * Modification  History: 
 * Date         Author        Version        Discription 
 * ----------------------------------------------------------------------------------- 
 * 2017-3-29       hnkeystone.com          1.0             1.0 
 * Why & What is modified: <修改原因描述> 
 */  
package com.fh.controller.me;

import java.io.UnsupportedEncodingException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.fh.controller.base.BaseController;
import com.fh.entity.system.User;
import com.fh.service.me.SupplierUserService;
import com.fh.util.MD5;
import com.fh.util.PageData;
import com.fh.util.SessionAreaUtil;
/** 
 * @ClassName:     Me 
 * @Description:TODO(这里用一句话描述这个类的作用) 
 * @author:    冯辉 
 * @date:        2017-3-29 下午6:18:11 
 *  
 */
@Controller
@RequestMapping(value = "/me")
public class Me extends BaseController {
	@Resource(name="supplierUserService")
	private SupplierUserService supplierUserService;
	/**
	 * 基本资料
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/masterinfo")
	public ModelAndView auditsetting()throws Exception{
		ModelAndView mv = this.getModelAndView();
		User user = SessionAreaUtil.getSessionUser();
		PageData pd = new PageData();
		pd.put("id", user.getSupplierid());
		supplierUserService.findUserById(pd);
		mv.setViewName("me/masterinfo");
		mv.addObject("pd",pd);
		return mv;
	}
	/**
	 * 修改资料页面
	 * @return
	 */
	@RequestMapping(value="/updateInformation")
	public ModelAndView updateInformation()throws Exception{
		ModelAndView mv = this.getModelAndView();
		User user = SessionAreaUtil.getSessionUser();
		PageData pd = new PageData();
		pd.put("id", user.getSupplierid());
		supplierUserService.findUserById(pd);
		mv.setViewName("me/update");
		mv.addObject("pd",pd);
		return mv;
	}
	
	/**
	 * 到修改资料成功页面
	 * @return
	
	 */
	@RequestMapping(value="/sureUpdate")
	public String sureUpdate(HttpServletRequest request,HttpSession session,String id){
		try {
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		String name=request.getParameter("name");
		String logo=request.getParameter("logo");
		String head=request.getParameter("head");
		String address=request.getParameter("address");
		String mobile=request.getParameter("mobile");
		String industry=request.getParameter("industry");
		String certificate=request.getParameter("certificate");
		String certificateNo=request.getParameter("certificateNo");
		String certificateImage=request.getParameter("certificateImage");
		
		
		PageData pd=new PageData();
		pd.put("id", id);
		pd.put("name", name);
		pd.put("logo", logo);
		pd.put("head", head);
		pd.put("address", address);
		pd.put("mobile", mobile);
		pd.put("industry", industry);
		pd.put("certificate", certificate);
		pd.put("certificateNo", certificateNo);
		pd.put("certificateImage", certificateImage);
		
		try {
			supplierUserService.updateUserByUId(pd);
			/*if(count==1){
				result=JsonResult.success("200", "修改成功");
			}else{
				result=JsonResult.error("300", "修改失败");
			}*/
		}catch (Exception e){
			e.printStackTrace();
		}
        
		return "redirect:/me/masterinfo";

	}
	
	/**
	 * 消息列表
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/messagelist")
	public ModelAndView messagelist()throws Exception{
		ModelAndView mv = this.getModelAndView();
		mv.setViewName("me/update");
		return mv;
	}

	/**
	 * 修改密码
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/passwordmodify")
	public ModelAndView passwordmodify()throws Exception{
		ModelAndView mv = this.getModelAndView();
		User user = SessionAreaUtil.getSessionUser();
		PageData pd = new PageData();
		pd.put("id", user.getSupplierid());
		mv.setViewName("me/passwordmodify");
		mv.addObject("pd",pd);
		return mv;
	}
	/**
	 *确认修改密码
	 * @param id
	 * @param password
	 * @return
	 */
		@RequestMapping("/sureModify")
		public String sureModify(String id,String password){
			PageData pd=this.getPageData();
			pd.put("id", id);
			//对密码加密
			pd.put("password", MD5.md5(password));
			
			if(StringUtils.isNotBlank(id)){
				try {
				supplierUserService.updatePass(pd);//更改密码
				} catch (Exception e) {
					logger.error(e.toString(), e);
				}
			}
			return "me/success";
		}
	/**
	 * 供应商申请
	 * @param model
	 * @return
	 */
	
	@RequestMapping(value="/supplierapply")
	public String applySupplier(Integer status,HttpSession session,String id){
		User user = SessionAreaUtil.getSessionUser();
		PageData pd=new PageData();
		pd.put("id", user.getSupplierid());
		try {
			User pda=supplierUserService.findSupplier(pd);
			if("0".equals(pda.getReviewstatus())){
				return "redirect:/me/masterinfo";
			}else if("1".equals(pda.getReviewstatus())){
				return "redirect:me/audit";
			}else if("2".equals(pda.getReviewstatus())){
				return "redirect:/me/supplierapply";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
		
	}
		
		/*return null;
		mv.setViewName("me/supplierapply");
		return mv;*/
	
	
	
}